package com.example;

import com.example.entity.Message;
//import org.just.computer.mathproject.Bean.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.security.Principal;
import java.util.Date;

@Controller
@CrossOrigin
public class GreetingController {
    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Message greeting(String content, Principal pl) throws Exception{
        try{
            System.out.println("Principal pl");
            System.out.println(pl);
            System.out.println("content");
            System.out.println(content);
            Message message = new Message();
            message.setContent(content.substring(1,content.length()-1));
            message.setData(new Date().toString());
        if(pl==null){
            message.setName("空的");
        }else{
            message.setName(pl.getName());
        }

            return message;
        }catch (Exception e){
            e.printStackTrace();
            Message message = new Message();
            message.setContent("错误");
            return message;
        }

    }
}
